package net.xju.controller;

import net.xju.common.framework.simplemvc.RequestMapping;
import net.xju.common.utils.JSONUtil;
import net.xju.common.utils.Result;
import net.xju.entity.Admin;
import net.xju.entity.Consultant;
import net.xju.entity.User;
import net.xju.service.AdminService;
import net.xju.service.ConsultantService;
import net.xju.service.UserService;
import net.xju.service.impl.AdminServiceImpl;
import net.xju.service.impl.ConsultantServiceImpl;
import net.xju.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

@RequestMapping("/api/login")
public class LoginController {
    private AdminService adminService = new AdminServiceImpl();
    private ConsultantService consultantService = new ConsultantServiceImpl();
    private UserService userService = new UserServiceImpl();

    @RequestMapping("/admin.do")
    public void adminLogin(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Admin admin = adminService.login(username, password);
        if (admin != null) {
            admin.setRole("admin"); // 确保有role字段
            HttpSession session = req.getSession();
            session.setAttribute("user", admin);
            JSONUtil.printByJSON(resp, Result.success(admin, "管理员登录成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("管理员账号或密码有误"));
        }
    }

    @RequestMapping("/consultant.do")
    public void consultantLogin(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Consultant consultant = consultantService.login(username, password);
        if (consultant != null) {
            consultant.setRole("consultant"); // 新增
            HttpSession session = req.getSession();
            session.setAttribute("user", consultant);
            JSONUtil.printByJSON(resp, Result.success(consultant, "咨询师登录成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("咨询师账号或密码有误"));
        }
    }

    @RequestMapping("/user.do")
    public void userLogin(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = userService.login(username, password);
        if (user != null) {
            user.setRole("user"); // 新增
            HttpSession session = req.getSession();
            session.setAttribute("user", user);
            JSONUtil.printByJSON(resp, Result.success(user, "用户登录成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("用户账号或密码有误"));
        }
    }
}